package com.example.springbootsfzy.controller.liujinping;

import com.example.springbootsfzy.domain.Announcements;
import com.example.springbootsfzy.service.liujinping.AnnouncementsService;
import com.example.springbootsfzy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
//公告表
@RestController
@RequestMapping("/announcements")
public class AnnouncementsController {
    //公告管理
    @Autowired
    private AnnouncementsService announcementsService;
    /*
     *查询
     */
    @GetMapping("/getAnnouncement")
    public R getAnnouncement(Long id) {
        return R.ok(announcementsService.getAnnouncements(id));
    }
    /*
     *查询所有
     */
    @GetMapping("/getAnnouncementsList")
    public R getAnnouncementsList() {
        return R.ok(announcementsService.getAllAnnouncements());
    }

    @GetMapping("/selectByConsultantId/{consultantId}")
    public R selectByConsultantId(
            @PathVariable int consultantId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "5") int pageSize) {
        List<Announcements> announcements = announcementsService.selectByConsultantId(consultantId, pageNum, pageSize);
        return R.ok(announcements);
    }

    /*
     *添加
     */
    @PostMapping
    public R addAnnouncements(@RequestBody Announcements announcements) {
        int result = announcementsService.addAnnouncements(announcements);
        if (result > 0) {
            return R.ok("添加成功");
        }
        return R.error("添加失败");
    }

    /*
     *删除
     */
    @DeleteMapping
    public R deleteAnnouncements(Long id) {
        int result = announcementsService.deleteAnnouncements(id);
        if (result > 0) {
            return R.ok("删除成功");
        }
        return R.error("删除失败");
    }

    /*
     *修改
     */
    @PutMapping
    public R updateAnnouncements(@RequestBody Announcements announcements) {
        int result = announcementsService.updateAnnouncements(announcements);
        if (result > 0) {
            return R.ok("修改成功");
        }
        return R.error("修改失败");
    }
    @PatchMapping
    public R updateAnnouncementsPatch(@RequestBody Announcements announcements) {
        int result = announcementsService.updateAnnouncementsActive(announcements);
        if (result > 0) {
            return R.ok("修改成功");
        }
        return R.error("修改失败");
    }

}
